package com.koron.tool.frame.bean;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.koron.util.Util;

public class InsertBean {
	File file;
	private String content;
	private String id;
	private String parameterType;
	private String useGeneratedKeys;
	private String keyProperty;
	private String nameSpace;
	public InsertBean(File file, String id, String content,String nameSpace) {
		this.file = file;
		this.id = id;
		this.nameSpace = nameSpace;
		useGeneratedKeys = Util.getAttribute(content,"useGeneratedKeys");
		parameterType = Util.getAttribute(content,"parameterType");
		keyProperty = Util.getAttribute(content,"keyProperty");
		Pattern pattern = Pattern.compile("<insert[^>]*>([\\d\\D]*?)</insert>");
		Matcher m = pattern.matcher(content);
		if (m.find())
			this.content = m.group(1);
	}

	/**
	 * 把内容写入XML文件中
	 * 
	 * @return 写入是否成功
	 * @throws IOException
	 */
	public boolean saveToXml(String oId) throws IOException {
		String str = "<insert [^>]*id=\\\"" + oId + "\\\"[^>]*>([\\d\\D]*?)</insert>";
		String encode = Util.getXmlFileEncode(file);
		String fileStr = Util.fileToString(file, encode);
		Matcher m = Pattern.compile(str).matcher(fileStr);
		if (m.find()) {
			StringBuilder sb = new StringBuilder(fileStr);
			// 找出的insert标签里的字符串
			StringBuilder context = new StringBuilder(m.group());
			context.replace(m.start(1) - m.start(), m.end(1) - m.start(), content);
			CharSequence cont = Util.setAttribute(context, "parameterType", parameterType);
			cont = Util.setAttribute(cont, "id", id);
			if (useGeneratedKeys.equals("false")) {
				cont = Util.setAttribute(cont, "useGeneratedKeys", "");
				cont = Util.setAttribute(cont, "keyProperty", "");
			} else {
				cont = Util.setAttribute(cont, "useGeneratedKeys", useGeneratedKeys);
				cont = Util.setAttribute(cont, "keyProperty", keyProperty);
			}

			sb.replace(m.start(), m.end(), cont.toString());
			PrintWriter pw = new PrintWriter(file, encode);
			pw.print(sb.toString());
			pw.close();
			return true;
		}
		return false;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getParameterType() {
		return parameterType;
	}

	public void setParameterType(String parameterType) {
		this.parameterType = parameterType;
	}

	public String getUseGeneratedKeys() {
		return useGeneratedKeys;
	}

	public void setUseGeneratedKeys(String useGeneratedKeys) {
		this.useGeneratedKeys = useGeneratedKeys;
	}

	public String getKeyProperty() {
		return keyProperty;
	}

	public void setKeyProperty(String keyProperty) {
		this.keyProperty = keyProperty;
	}

	/**
	 * @return 获取#{bare_field_comment}
	 */
	public File getFile() {
		return file;
	}

	/**
	 * @param 设置#{bare_field_comment}
	 */
	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * @return 获取#{bare_field_comment}
	 */
	public String getNameSpace() {
		return nameSpace;
	}

	/**
	 * @param 设置#{bare_field_comment}
	 */
	public void setNameSpace(String nameSpace) {
		this.nameSpace = nameSpace;
	}
	
}